package com.lin.missyou.api.v1;

import com.lin.missyou.core.LocalUser;
import com.lin.missyou.core.enumeration.CouponStatus;
import com.lin.missyou.core.interceptors.ScopeLevel;
import com.lin.missyou.exception.CreateSuccess;
import com.lin.missyou.exception.http.ParameterException;
import com.lin.missyou.model.Coupon;
import com.lin.missyou.service.CouponService;
import com.lin.missyou.vo.CouponCategoryVo;
import com.lin.missyou.vo.CouponPureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gcq
 * @Create 2021-11-03
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    /**
     * 领取指定分类卷
     * @param cid
     * @return
     */
    @GetMapping("/by/category/{cid}")
    public List<CouponPureVo> getCouponListByCategory(@PathVariable Long cid)
    {
        List<Coupon> coupons = couponService.getByCategory(cid);
        if(coupons.isEmpty()){
            return Collections.emptyList();
        }
        List<CouponPureVo> vos = CouponPureVo.getList(coupons);
        return vos;
    }

    /**
     * 查询全场卷
     * @return
     */
    @GetMapping("/whole_store")
    public List<CouponPureVo> getWholeStoreCouponList()
    {
        List<Coupon> coupons = this.couponService.getWholeStoreCoupons();
        if(coupons.isEmpty())
        {
            return Collections.emptyList();
        }
        return CouponPureVo.getList(coupons);
    }

    /**
     * 领取优惠券
     * 新增模型返回前端意义不大
     * @param id 优惠卷id
     */
    @ScopeLevel()
    @PostMapping("/collect/{id}")
    public void collectCoupon(@PathVariable Long id)
    {
        Long uid = LocalUser.getUser().getId();
        couponService.collectOneCoupon(uid, id);
        // 静态类抛出异常
        throw new CreateSuccess(0);
    }


    /**
     * 获取对应状态的优惠券
     * @param status
     * @return
     */
    @ScopeLevel
    @GetMapping("/myself/by/status/{status}")
    public List<CouponPureVo> getMyCouponByStatus(@PathVariable Integer status)
    {
        Long uid = LocalUser.getUser().getId();
        List<Coupon> couponList = new ArrayList<>();
        switch (CouponStatus.toType(status)){
            case AVAILABLE:
                couponList = couponService.getMyAvailableCoupons(uid);
                break;
            case USED:
                couponList = couponService.getMyUsedCoupons(uid);
                break;
            case EXPIRED:
                couponList = couponService.getMyExpiredCoupons(uid);
                break;
            default:
                throw new ParameterException(40001);
        }
        return CouponPureVo.getList(couponList);
    }


    /**
     * 查询可用的优惠券
     * @return
     */
    @ScopeLevel
    @GetMapping("/myself/available/with_category")
    public List<CouponCategoryVo> getUserCouponWithCategory()
    {
        Long uid = LocalUser.getUser().getId();
        List<Coupon> coupons = couponService.getMyAvailableCoupons(uid);
        if(coupons.isEmpty())
        {
            return Collections.emptyList();
        }
        return coupons.stream().map(coupon -> {
            CouponCategoryVo vo = new CouponCategoryVo(coupon);
            return vo;
        }).collect(Collectors.toList());
    }
}